home *** CD-ROM | disk | FTP | other *** search
- /* level 2 I/O functions that will work with sockets or files */
-
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <bsdsocket.h>
-
- /* assume that we won't have more than 4096 sockets,
- * & that FILE* must be greater than 4095 */
-
- #define IS_SOCKET(x) (!(((ULONG)x)&0xfffff000))
-
- /* socket or file replacement for fprintf */
-
- int
- __SaFprintf( FILE * fd, char * template, ... )
- {
- va_list arglist;
- int length;
-
- va_start( arglist, template );
-
- if( IS_SOCKET( fd ) )
- {
- char buffer[8092];
-
- length = vsprintf( buffer, template, arglist );
-
- send( (int)fd, buffer, length, NULL );
- }
- else
- length = vfprintf( fd, template, arglist );
-
- va_end( arglist );
-
- return( length );
- }
-
- int
- __SaFwrite( char * buffer, size_t length, size_t n, FILE * fd )
- {
- int ret;
-
- if( IS_SOCKET( fd ) )
- {
- if( length == 1 )
- {
- length = n;
- n = 1;
- }
-
- ret = (send( (int)fd, buffer, length, NULL ) == length);
- }
- else
- ret = fwrite( buffer, length, n, fd );
-
- return( ret );
- }
-
- int
- __SaFputs( char * string, FILE * fd )
- {
- int ret;
-
- if( IS_SOCKET( fd ) )
- {
- int length = strlen( string );
-
- ret = (send( (int)fd, string, length, NULL ) == length) ?
- 0 : EOF;
- }
- else
- ret = fputs( string, fd );
-
- return( ret );
- }
-
- int
- __SaFputc( char byte, FILE * fd )
- {
- int ret;
-
- if( IS_SOCKET( fd ) )
- ret = (send( (int)fd, &byte, 1, NULL ) == 1) ?
- byte : EOF;
- else
- ret = fputc( byte, fd );
-
- return( ret );
- }
-
- int
- __SaFclose( FILE * fd )
- {
- int ret;
-
- if( IS_SOCKET( fd ) )
- ret = CloseSocket( (int)fd );
- else
- ret = fclose( fd );
-
- return( ret );
- }
-
- int
- __SaFflush( FILE * fd )
- {
- int ret;
-
- if( ! IS_SOCKET( fd ) )
- ret = fflush( fd );
-
- return( ret );
- }
-
- /* Dummy alarm function */
-
- void
- alarm( int timeout )
- {
- }
-
- /* Dummy getpid function */
-
- int
- getpid( void )
- {
- return( 0 );
- }
-
- /* Dummy crypt function */
-
- char *
- crypt( char * sent, char * real )
- {
- return( sent );
- }
-